﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Mobile.Mvc;

namespace MVCDemoClient
{
    public partial class ProductEditControl : ViewControl, IView<NorthwindDataSet.ProductsRow>
    {        
        [PublishEvent("OnProductUpdated")]
        public event EventHandler ProductUpdatedEvent;

        public ProductEditControl()
        {
            InitializeComponent();
        }

        // Callback from the Controller
        private void OnProductLoaded(object sender, EventArgs e)
        {           
            this.txtProductName.Text = this.ViewData.Model.ProductName;
            this.txtUnitPrice.Text = this.ViewData.Model.UnitPrice.ToString();
        }

        #region IView<ProductsRow> Members

        public new ViewDataDictionary<NorthwindDataSet.ProductsRow> ViewData
        {
            get;
            set;
        }

        public new NorthwindDataSet.ProductsRow Model
        {
            get;
            set;
        }

        #endregion

        private void cmdUpdate_Click(object sender, EventArgs e)
        {
            // Assign the changed values back to the model data
            this.ViewData.Model.ProductName = this.txtProductName.Text;
            this.ViewData.Model.UnitPrice = Convert.ToDecimal(this.txtUnitPrice.Text);
            // Notify the controller
            if (this.ProductUpdatedEvent != null)
            {
                this.ProductUpdatedEvent(this, EventArgs.Empty);
            }
        }

        private void cmdCancel_Click(object sender, EventArgs e)
        {

        }
       
    }
}
